﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LeanAndon.Silverlight
{
    using System.ComponentModel;
    using System.Globalization;

    public partial class MainPage : UserControl
    {
        object syncRoot = new object();
        // PubSubClient client = null;

        public MainPage()
        {
            InitializeComponent();
        }

        private void SubscribeButton_Click(object sender, RoutedEventArgs e)
        {
        //    // this.TopicName.IsEnabled = this.SubscribeButton.IsEnabled = false;
        //    // this.PublishText.IsEnabled = true;
        //    // this.Notifications.Text = "";

        //    this.client = new PubSubClient(
        //        new PollingDuplexHttpBinding(),
        //        new EndpointAddress(
        //            "http://"
        //            + App.Current.Host.Source.DnsSafeHost
        //            + ":"
        //            + App.Current.Host.Source.Port.ToString(CultureInfo.InvariantCulture)
        //            + "/PubSubService.svc"));
        //    this.client.NotifyReceived += new EventHandler<NotifyReceivedEventArgs>(client_NotifyReceived);
        //    this.client.PublishCompleted += new EventHandler<AsyncCompletedEventArgs>(client_PublishCompleted);
        //    this.client.SubscribeCompleted += new EventHandler<AsyncCompletedEventArgs>(client_SubscribeCompleted);

        //    this.client.SubscribeAsync(this.TopicName.Text);
        //}

        //void client_SubscribeCompleted(object sender, AsyncCompletedEventArgs e)
        //{
        //    if (!this.IsError(e))
        //    {
        //        this.AddNotification("CLIENT ACTION: Subscribed to topic " + this.TopicName.Text);
        //    }
        //}

        //bool IsError(AsyncCompletedEventArgs e)
        //{
        //    if (e.Error != null)
        //    {
        //        lock (this.syncRoot)
        //        {
        //            this.client.CloseAsync();
        //            this.client = null;
        //        }
        //        this.AddNotification("ERROR: " + e.Error.Message);
        //        this.TopicName.IsEnabled = this.SubscribeButton.IsEnabled = true;
        //        this.PublishText.IsEnabled = false;
        //    }

        //    return e.Error != null;
        }
    }
}
